﻿using System;
using System.Collections.Generic;
using System.Text;

namespace BL
{
	public class Subjects
	{
		static Subjects instance;
		public static Subjects Instance
		{
			get { return instance; }
		}

		List<SubjectItem> items = new List<SubjectItem>();
        long maxID = 0;

		public Subjects(List<Entity> subjectItems)
		{
			instance = this;
            foreach (Entity e in subjectItems)
            {
                items.Add((SubjectItem)e);
                if (maxID < e.ID)
                    maxID = e.ID;
            }
            maxID++;
		}

		public SubjectItem[] Items
		{
			get { return items.ToArray(); }
		}

		public SubjectItem this[string str]
		{
			get
			{
				foreach (SubjectItem si in items)
				{
					if (si.ToString() == str)
						return si;
				}
				return null;
			}
		}

        public SubjectItem this[long id]
        {
            get
            {
				foreach (SubjectItem si in items)
				{
					if (si.ID == id)
						return si;
				}
				return null;
            }
            set
            {
                for (int i = 0; i < items.Count; i++)
                {
                    if (items[i].ID == id)
                    {
                        items[i].Faculty = value.Faculty;
                        items[i].Group = value.Group;
                        items[i].Hours = value.Hours;
                        items[i].LessonType = value.LessonType;
                        items[i].Subject = value.Subject;
                        return;
                    }
                }
            }
        }

        public long FreeID
        {
            get { return maxID; }
        }

        public void Add(SubjectItem si)
        {
            items.Add(si);
            if (maxID < si.ID)
                maxID = si.ID;
            maxID++;
        }
        public void Delete(long id)
        {
            Table.Instance.Delete(id);
            SubjectItem si = this[id];
            items.Remove(si);
        }

	}
}
